//
// Created by 86138 on 2025/7/18.
//
#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        int pre = 0;
        int count = 0;
        unordered_map<int, int> map;
        map[0] = 1;
        for (int i = 0; i < nums.size(); i++) {
            pre += nums[i];
            if (map.find(pre - k) != map.end()) {
                count += map[pre - k];
            }
            map[pre]++;
        }

        return count;
    }
};

